/*
 * BORLAND WITH GTK
 */

#include <dlfcn.h>
#include <stdio.h>
#include <TApplication.h>
#pragma hdrstop

#include "testgtk.h"
#pragma package(smart_init)
#pragma resource "*.xfm"


TForm1 * Form1;


__fastcall TForm1::TForm1 (TComponent *Owner) : TForm (Owner)
{
    printf ("TForm1::TForm1\n");
}

void __fastcall TForm1::Button1OnClick (TObject * Sender)
{
    printf ("TForm1::Button1Clicked!\n");
    
    Button1->Caption = "Button1 Clicked!";
}

void __fastcall TForm1::Button2OnClick (TObject * Sender)
{
    printf ("TForm1::Button2Clicked!\n");
    
    Application->Terminate ();
}

void __fastcall PACKAGE meuregistrador ()
{
    printf ("registrador ...\n");
}

unsigned handle;
typedef int (__stdcall * TRegisterProc) ();

namespace Classes {
    void __fastcall (* RegisterComponentsProc) (const AnsiString Page, const TMetaClass * const * ComponentClasses, const int ComponentClasses_Size);
    void __fastcall (* RegisterNoIconProc) (const TMetaClass * const * ComponentClasses, const int ComponentClasses_Size);
};

void __fastcall MyRegisterComponentsProc (const AnsiString Page, const TMetaClass * const * ComponentClasses, const int ComponentClasses_Size)
{
    int i;
    AnsiString Name;
    
    printf ("MyRegisterComponentsProc\n");
    
    for (i = 0; i <= ComponentClasses_Size; i++)
    {
	Name = ComponentClasses [i]->ClassName ();
	printf ("Page %s add %s\n", Page.c_str(), Name.c_str ());
    }
}

void __fastcall MyRegisterNoIconProc (const TMetaClass * const * ComponentClasses, const int ComponentClasses_Size)
{
}

void __fastcall InfoProc (const AnsiString Name, TNameType NameType, Byte Flags, void * Param)
{
    unsigned Module;
    
    printf ("InfoProc : %s ", Name.c_str ());
    
    switch (NameType)
    {
    case ntContainsUnit:
    {
	printf ("[ntContainsUnit]\n");
	
	AnsiString Proc = "@" + Name + "@Register$qqrv";
	TRegisterProc RegisterProc = (TRegisterProc) GetProcAddress (handle, Proc.c_str ());
	
	if (RegisterProc)
	{
	    printf ("+ Register %s\n", Name.c_str ());
	    try { RegisterProc (); } catch (...) {}
	    return;
	}
	
	break;
    }
    case ntRequiresPackage:
    {
	printf ("[ntRequiresPackage]\n");
	
	Module = LoadPackage (Name.c_str ());
	InitializePackage (Module);
	
	break;
    }
    case ntDcpBpiName:
    {
	printf ("[ntDcpBpiName]\n");
	break;
    }
    };
}

int
main (int argc, char **argv)
{
    int flags = 0;
    
    Classes::RegisterComponentsProc = MyRegisterComponentsProc;
    Classes::RegisterNoIconProc = MyRegisterNoIconProc;
    
    handle = LoadPackage ("/usr/local/vpe/packages/visual/bplvisual.so");
    GetPackageInfo (handle, NULL, flags, InfoProc);
    
    try
    {
    Application->Initialize ();
    
    // Application->CreateForm (__classid (TForm1), &Form1);
    
    Form1 = new TForm1 (Application);
    Form1->ShowAll ();
    
    printf ("Form1->Name = %s\n", Form1->Name);
    printf ("Form1->Caption = %s\n", ((AnsiString) Form1->Caption).c_str ());
    printf ("Form1->Button1->Caption = %s\n", ((AnsiString) Form1->Button1->Caption).c_str ());
    printf ("Form1->Button2->Caption = %s\n", ((AnsiString) Form1->Button2->Caption).c_str ());
    
    Application->Run ();
    }
    catch (Exception &exception)
    {
        Application->ShowException (&exception);
    }
    catch (...)
    {
        try
        {
            throw Exception ("");
        }
        catch (Exception &exception)
        {
            Application->ShowException (&exception);
        }
    }
    
    return 0;
}
